package com.webside.distinguish.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.webside.base.baseservice.impl.AbstractService;
import com.webside.distinguish.mapper.SeriesDistinguishMapper;
import com.webside.distinguish.model.SeriesDistinguish;
import com.webside.distinguish.model.SeriesDistinguishKey;
import com.webside.distinguish.service.SeriesDistinguishService;
import com.webside.interfaces.model.SeriesModel;
import com.webside.interfaces.model.SeriesVideoTree;
@Service("seriesDistinguishService")
public class SeriesDistinguishServiceImpl extends AbstractService<SeriesDistinguish, SeriesDistinguishKey> implements SeriesDistinguishService{

	@Autowired
	SeriesDistinguishMapper seriesDistinguishMapper;
	
	@Autowired
	public void setBaseMapper(){
		super.setBaseMapper(seriesDistinguishMapper);
	}
	
	@Override
	public List<SeriesDistinguish> selectListByDistinguishId(Long distinguishId) {
		return seriesDistinguishMapper.selectByDistinguishId(distinguishId);
	}

	@Override
	public int add(SeriesDistinguishKey sdk) {
		SeriesDistinguish sd = new SeriesDistinguish();
		sd.setDistinguishId(sdk.getDistinguishId());
		sd.setSeriesId(sdk.getSeriesId());
		sd.setCreateTime(new Date());
		sd.setDeleteState(true);
		int insert = seriesDistinguishMapper.insert(sd);
		return insert;
	}

	@Override
	public SeriesDistinguish SelectByPrimaryKey(SeriesDistinguishKey sdk) {
		// TODO Auto-generated method stub
		return seriesDistinguishMapper.selectByPrimaryKey(sdk);
	}
	
	@Override
	public int deleteById(SeriesDistinguishKey sdk){
		return seriesDistinguishMapper.deleteByPrimaryKey(sdk);
	}

	@Override
	public int addList(List<SeriesDistinguishKey> sdkList) {
		List<SeriesDistinguish> sdList = new ArrayList<SeriesDistinguish>();
		for (int i = 0; i < sdkList.size(); i++) {
			SeriesDistinguish sd = new SeriesDistinguish();
			sd.setDistinguishId(sdkList.get(i).getDistinguishId());
			sd.setSeriesId(sdkList.get(i).getSeriesId());
			sd.setCreateTime(new Date());
			sd.setDeleteState(true);
			sdList.add(sd);
		}
		int insert = seriesDistinguishMapper.insertBatch(sdList);
		return insert;
	}
	
	public int deleteByIds(Long distinguishId, List<String> sdkList){

		return seriesDistinguishMapper.deleteBatchById(distinguishId,sdkList);
	}

	@Override
	public List<SeriesModel> selectSeriesListByAppid(String appid,String channel,Integer pageNo) {
		// TODO Auto-generated method stub
		/*PageHelper.startPage(3, 50);
		PageHelper.orderBy("id");*/
		if (pageNo < 1) {
			pageNo = 1;
		}
		Integer start = (pageNo - 1) * 50 + 1;
		
		List<SeriesModel> list = seriesDistinguishMapper.selectSeriesListByAppid(appid,channel,start);
		return list;
	}
	
	@Override
	public List<SeriesModel> selectOfflineSeriesListByAppid(String appid,String channel,Integer pageNo) {
		// TODO Auto-generated method stub
		/*PageHelper.startPage(3, 50);
		PageHelper.orderBy("id");*/
		if (pageNo < 1) {
			pageNo = 1;
		}
		Integer start = (pageNo - 1) * 50 + 1;
		
		List<SeriesModel> list = seriesDistinguishMapper.selectOfflineSeriesListByAppid(appid,channel,start);
		return list;
	}

	@Override
	public int selectCountByAppid(String appid,String channel,Integer pageNo) {
		// TODO Auto-generated method stub
		
		int count = seriesDistinguishMapper.selectCountByAppid(appid,channel);
		
		int totalPage = (int) Math.ceil((double)count/50);
		
		return totalPage;
	}
	
	@Override
	public int selectOfflineCountByAppid(String appid,String channel,Integer pageNo) {
		// TODO Auto-generated method stub
		
		int count = seriesDistinguishMapper.selectOfflineCountByAppid(appid,channel);
		
		int totalPage = (int) Math.ceil((double)count/50);
		
		return totalPage;
	}

	@Override
	public SeriesVideoTree selectBySeriesId(Integer seriesId) {
		// TODO Auto-generated method stub
		return seriesDistinguishMapper.selectBySeriesId(seriesId);
	}

}
